<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ArticleSelected extends Model
{
    protected $table="article_selected";

    public function addOne($today, $article_id, $opertion_user_id, $del=0){
    	$one = $this->where("today","=", $today)->first();
    	if(!$one){
    		$item = new self;
    		$item->today = $today;
    		$item->article_ids = $article_id;
    		$item->today_timestamp = strtotime($today);
    		$item->opertion_user_id= $opertion_user_id;
    		$item->save();
    	}else{
            if($del==1){
                $ids = explode(",", $one->article_ids);
                foreach($ids as $k=>$id){
                    if($id==$article_id){
                        unset($ids[$k]);
                    }
                }
                if(empty($ids)){
                    $one->delete();
                    return;
                }
                //dd($ids);
                //$ids = array_diff($ids, $article_id);
                $one->article_ids = implode(',', $ids);
            }else{
                $one->article_ids = $one->article_ids.','.$article_id;
            }
             $one->save();
    	}
    }
}
